/*
 * @Description:
 * @Author: mmzhang
 * @Date: 2022-02-09 15:50:16
 * @LastEditTime: 2023-01-11 14:02:59
 */
import buildingApi from '@/config/server/api/building'
import channelApi from '@/config/server/api/base.js'
import tool from '@/common/tool.js'
import dt from '@dt/dt'

let houses = []
let roomList = []
let channelList = []

function getHouse(housesName, isShowCity = false) {
  return buildingApi.house.names
    .fetch({
      housesName
    })
    .then((res) => {
      houses = res.list.map((item) => {
        item.key = item.housesId + '-' + item.orgId
        item.value = isShowCity
          ? `${item.housesName} ${item.orgName} ${item.city ? '-' + item.city : ''}`
          : item.housesName + '（' + item.orgName + '）'
        return item
      })
      return houses
    })
}

function create(opt = {}) {
  // key to
  return {
    title: opt.title || '案场名称',
    key: opt.key || 'housesId',
    placeholder: opt.placeholder || '请输入案场名称',
    isBuild: true,
    select: {
      clearable: true,
      filterable: true,
      options: houses.length > 0 ? houses : opt.options || [],
      remote: (key) => {
        return getHouse(key, opt.isShowCity)
      }
    },
    disabled: opt.disabled,
    change: opt.change,
    visible: opt.visible == false ? false : true,
    data: {
      from: (info) => {
        if (info[opt.key || 'housesId'] && info.companyId && info.housesName) {
          return info[opt.key || 'housesId'] + '-' + info.companyId
        }
        return null
      },
      to: (v) => {
        let info = {
          housesId: null,
          housesName: null,
          companyId: null,
          companyName: null
        }
        if (v) {
          let vs = v.value.split('-')
          let ls = v.label.replace('）', '').split('（')
          info.housesId = Number(vs[0])
          info.housesName = ls[0]
          info.companyId = Number(vs[1])
          info.companyName = ls[1]
        }
        if (opt.to) {
          return opt.to(info)
        }
        return info
      }
    },
    rules: opt.rules || [
      {
        required: true,
        type: 'number',
        message: '请输入案场名称',
        trigger: 'blur'
      }
    ]
  }
}

function getRoom(roomName, buildingId = '') {
  let data = {
    roomName
  }
  if (tool.getStorage('isbuildFlag') == 'buildpage') {
    data.buildingId = dt.buildInfo().housesId
  } else {
    data.buildingId = buildingId
  }
  data.current = 1
  data.page = 1
  data.rows = 20
  return buildingApi.roomScreen.fetch(data).then((res) => {
    roomList = res.list.map((item) => {
      item.key = item.roomId
      item.value = item.roomName
      return item
    })
    return roomList
  })
}

function createRoom(opt = {}) {
  // key to
  return {
    title: opt.title || '房源名称',
    key: opt.key || 'roomId',
    placeholder: opt.placeholder || '请输入房源名称',
    select: {
      clearable: true,
      filterable: true,
      options: roomList.length > 0 ? roomList : opt.options || [],
      remote: (key) => {
        return getRoom(key, opt.buildingId)
      }
    },
    change: opt.change
  }
}

function getChannel(channelName) {
  let data = {
    channelName
  }
  if (tool.getStorage('isbuildFlag') == 'buildpage') {
    data.buildingId = dt.buildInfo().housesId
  }
  return channelApi.selectBuildingChannelList.fetch(data).then((res) => {
    channelList = res.map((ele) => {
      ele.key = ele.channelId
      ele.value = ele.channelName
      return ele
    })
    return channelList
  })
}

function createChannel(opt = {}) {
  // key to
  return {
    title: opt.title || '渠道名称',
    key: opt.key || 'channelId',
    placeholder: opt.placeholder || '请输入渠道名称',
    col: opt.col || 24,
    select: {
      clearable: opt.clearable || true,
      filterable: opt.filterable || true,
      options: channelList.length > 0 ? channelList : opt.options || [],
      remote: (key) => {
        return getChannel(key)
      }
    },
    change: opt.change,
    rules: opt.rules || []
  }
}

export default {
  create,
  createRoom,
  createChannel
}
